﻿// Date: 22.09.11

// Revision 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NOVA.Utilities
{
    /// <summary>
    /// Event argument for the exception manager.
    /// </summary>
    public class ExpListArgs : EventArgs
    {
        public Exception CurrentException;

        public ExpListArgs(Exception exp)
            : base()
        {
            this.CurrentException = exp;
        }
    }

    /// <summary>
    /// Exception manager, which handles all critical exceptions of the NOVA framework
    /// and notifies all participating listeners.
    /// In debug mode, all incoming exceptions are thrown automatically.
    /// </summary>
    public static class ExceptionManager
    {
        #region Members
                
        static List<Exception> m_exceptions;
        public static event EventHandler<ExpListArgs> Listener;
        public static bool ModeDebug;
        
        #endregion
        
        #region Constructor

        /// <summary>
        /// Exception manager, which handles all critical exception of the NOVA framework.
        /// In debug mode, all incoming exception are thrown automatically.
        /// </summary>
        static ExceptionManager()
        {
            m_exceptions = new List<Exception>();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Adds a new exception to the manager and notifies the listeners. 
        /// If debug mode is enabled, then the exception will be thrown automatically.
        /// </summary>
        /// <param name="exp"></param>
        public static void Add(Exception exp)
        {
            // If it's a new (unknown) exception, add it to the list
            if(!m_exceptions.Contains(exp))
                m_exceptions.Add(exp);

            // If a listener is defined, notify him about the newest exception
            if (Listener != null)
                Listener(null, new ExpListArgs(m_exceptions[m_exceptions.Count-1]));

            // Throw the exception, if debug mode is activated
            if (ModeDebug)
                throw exp;
        }

        #endregion
    }
}
